<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const xhr = new XMLHttpRequest();
      xhr.open("POST", "http://127.0.0.1:3000/data");
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xhr.responseType = "json";
      xhr.onload = function () {
        console.log(xhr.response);
      };

      function _stringify(obj) {
        return Object.entries(obj)
          .reduce((str, el) => {
            console.log(el);
            return (str += `${el[0]}=${el[1]}&`);
          }, "")
          .slice(0, -1);
      }

      // username=jack&age=20
      function _parse(str) {
        return Object.fromEntries(str.split("&").map((el) => el.split("=")));
      }
      console.log(_parse("username=jack&age=20"));

      // 发送请求
      // xhr.send(
      //   _stringify({
      //     username: "jack",
      //     age: 20,
      //   })
      // );
    </script>
  </body>
</html>
